﻿using System;
using System.Data;
using BLL;
using BLL.Access;
using Web.Class;
using DevExpress.Web;
using YXYLCommon;

namespace Web.Pages.YongShui._1.JiChuXX
{
    public partial class YongHuCLCX : System.Web.UI.Page
    {
        #region 属性
        public System.Data.DataTable MyDataSource
        {
            get { return ViewState["MyDataSource"] as System.Data.DataTable; }
            set { ViewState["MyDataSource"] = value; }
        }
        #endregion

        #region 变量
        /// <summary>
        /// 存储过程调用
        /// </summary>
        SpHelper _sphelper = new SpHelper();
        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL BllYongHu = new YS_YG_YONGHUXXBLL();

        SYS_UPLOADProcess uploadbll = new SYS_UPLOADProcess();

        SYS_UPLOAD_LOGBLL uploadLOGBLL = new SYS_UPLOAD_LOGBLL();
        #endregion

        #region 事件
        
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
            if (IsCallback)
            {
                gvLH.DataSource = MyDataSource;
                gvLH.DataBind();
            }
        }

        protected void gvLH_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            if (!hdCollection.Contains("yonghuh"))
            {
                //return;
            }
            string where = "and i_jlzt>=0";
            if (hdCollection.Contains("yonghuh") && hdCollection.Get("yonghuh").ToString().Trim().Length>0)
            {
                where += " and S_YONGHUBH='" + hdCollection.Get("yonghuh") + "'";
            }
            if (hdCollection.Contains("cllb") && hdCollection.Get("cllb").ToString().Trim().Length > 0 && hdCollection.Get("cllb").ToString().Trim()!="-1")
            {
                where += " and i_wenjianlx=" + hdCollection.Get("cllb");
            }
            DateTime d;
            if (hdCollection.Contains("start") && hdCollection.Get("start").ToString().Trim().Length>0&&DateTime.TryParse(hdCollection.Get("start").ToString(),out d))
            {
                where += " and D_CAOZUORQ>=to_date('" + hdCollection.Get("start") + " 00:00:00','yyyy-MM-dd HH24:Mi:SS') ";                
            }
            if (hdCollection.Contains("end") && hdCollection.Get("end").ToString().Trim().Length > 0 && DateTime.TryParse(hdCollection.Get("end").ToString(), out d))
            {
                where += " and D_CAOZUORQ<=to_date('" + hdCollection.Get("end") + " 23:59:59','yyyy-MM-dd HH24:Mi:SS') ";
            }
            DataSet ds = _sphelper.P_YS_CX_CAILIAOXX(where + " order by id desc", 1);
            MyDataSource = ProcessImagePath(ds.Tables[0]); ;
            gvLH.DataSource = MyDataSource;
            gvLH.DataBind();
        }
        #endregion
        #region 方法
        private void InitControl()
        {
            gvLH.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            txtYongHuH.Focus();
            DataSet ds = _sphelper.P_YS_LH_CAILIAOLBCX(" and I_CAILIAOBH not in (1,2,3,4,5,14)");
            DataTable dt = ds.Tables[0];

            cmCaiLiaoLB.DataSource = ds.Tables[0];
            cmCaiLiaoLB.TextField = "S_CAILIAOMC";
            cmCaiLiaoLB.ValueField = "I_CAILIAOBH";
            cmCaiLiaoLB.DataBind();
            cmCaiLiaoLB.Items.Insert(0, new ListEditItem("全部", "-1"));
            cmCaiLiaoLB.SelectedIndex = 0;
            cmCaiLiaoLB.Items.Add("其他", 20);
            if (Request["yonghuh"] != null && Request["yonghuh"].Trim().Length > 0)
            {
                txtYongHuH.Text = Request["yonghuh"];
                DataSet ds1 = _sphelper.P_YS_CX_CAILIAOXX(" and S_YONGHUBH='" + Request["yonghuh"] + "' and i_jlzt>=0" + " order by id desc", 1);
                MyDataSource = ProcessImagePath(ds1.Tables[0]);
                gvLH.DataSource = MyDataSource;
                gvLH.DataBind();
            }
        }

        private DataTable ProcessImagePath(DataTable table)
        {
            DataTable infoTable = table;
            foreach (DataRow row in infoTable.Rows)
            {
                var host = Request.Url.ToString().Replace("http://", "").Split('/')[0];
                row["S_WENJIANLJ"] = "http://" + host + "/" + row["S_WENJIANLJ"];
                var filePath = row["S_WENJIANLJ"].ToString().ToLower();
                if (filePath.EndsWith("jpg") || filePath.EndsWith("png") || filePath.EndsWith("jpeg") ||
                    filePath.EndsWith("bmp"))
                {
                    row["S_WENJIANLJ"] = "http://" + host + "/ImageView.html?img=" + row["S_WENJIANLJ"];
                }
            }
            return infoTable;
        }
        #endregion

        //protected void Call_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        //{
        //    string result = "";
        //    if (e.Parameter == "Delete")
        //    {
        //        int id = hdCollection.Get("id").GetNoNullInt32();
        //        if (id > 0)
        //        {
        //            var s= uploadLOGBLL.SelectSYS_UPLOAD_LOG(id);
        //            UtilComm.CleanFiles(Server.MapPath(Request.ApplicationPath + s.S_WENJIANLJ));
                    
        //            uploadLOGBLL.DeleteSYS_UPLOAD_LOG(id);
        //            var s1 = uploadbll.SearchBywenjianlj(s);
        //            if (s1 != null)
        //            {
 
        //            }
        //            result = "1";
        //        }
        //    }
        //    e.Result = result;
        //}
        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            int id = this.hdCollection.Get("ID").GetNoNullInt32();
           if (e.Parameter == "del")
            {
                if (id > 0)
                {
                    //uploadbll.DeleteSYSUpLoadLog(id);
                    //uploadbll.DeleteSYSUpLoadLog(id);
                    new SpHelper().P_DELETESYSUPLOADLOG(id);
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "操作成功！";
                    e.Result = cr.ToString();
                    return;
                }
            }

        }
    }
}